/**
 * Copyright By Nanjing Fujitsu Nanda Software Technology Co., Ltd
 * 下午4:31:20
 * MyAuthenticationProvider.java
 * 
 */
package com.fujitsu.nanjing.tieba.security;

import com.fujitsu.nanjing.tieba.constrant.CommonParamter;

import org.springframework.security.authentication.BadCredentialsException;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.authentication.dao.DaoAuthenticationProvider;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.core.userdetails.UserDetails;

/**
 * @author Administrator
 *
 */
public class MyAuthenticationProvider extends DaoAuthenticationProvider {

    @Override
    protected void additionalAuthenticationChecks(UserDetails userDetails,
            UsernamePasswordAuthenticationToken authentication)
            throws AuthenticationException {
        Object salt = null;
        if (getSaltSource() != null)
            salt = getSaltSource().getSalt(userDetails);
        if (authentication.getCredentials() == null) {
            logger.debug("Authentication failed: no credentials provided");
            throw new BadCredentialsException(CommonParamter.PASSWORD_EMPTY);
        }
        String presentedPassword = authentication.getCredentials().toString();
        if (!getPasswordEncoder().isPasswordValid(userDetails.getPassword(),
                presentedPassword, salt)) {
            logger.debug("Authentication failed: password does not match stored value");
            throw new BadCredentialsException(CommonParamter.PASSWORD_ERROR);
        } else {
            return;
        }
    }

    @Override
    protected void doAfterPropertiesSet() throws Exception {

    }

}
